package manager.msales;

import java.io.*;
import common.db.*;
import common.main.*;
import java.util.*;


public class OrderListBean extends MainBean
{
	public void funcExecute() throws Exception
	{
		String date1 = (String)inputTable.get("date1");
		String date2 = (String)inputTable.get("date2");
		String id = (String)inputTable.get("id");
		String state = (String)inputTable.get("state");
		String hidden = (String)inputTable.get("hidden");
		String option1 = (String)inputTable.get("option1");
		String option2 = (String)inputTable.get("option2");
		String inputPage = (String)inputTable.get("inputPage");
		int unit= Integer.parseInt((String)inputTable.get("unit"));

		//queryBuffer = new StringBuffer("");
		//queryBuffer.append("SELECT MAX(SEQ) COUNT FROM MAGIC");
		//DBExecute dbe = new DBExecute(queryBuffer.toString());
		//outputList = dbe.getDataList();


		StringBuffer countQuery = new StringBuffer("");
		countQuery.append("SELECT MAX(a.DISP_NUM) COUNT FROM  ( SELECT ROWNUM DISP_NUM FROM MAGIC a, MEMBER b, PAY_KIND c  WHERE a.ID = b.ID(+) AND c.SEQ(+) = a.PAY_CODE ");

		queryBuffer = new StringBuffer("");
		queryBuffer.append("SELECT a.DISP_NUM, a.SEQ, a.ID, a.NAME, a.TIME, a.PAYMENT, a.USE_BONUS,  a.ORDER_STATE, A.DELIVERY_STATE, a.Pay_KIN FROM ( SELECT ROWNUM DISP_NUM, a.SEQ, a.ID, b.NAME, TIME, a.PAYMENT, a.USE_BONUS, ");
		queryBuffer.append("a.ORDER_STATE,  a.DELIVERY_STATE, c.PAY_KIN FROM MAGIC a, MEMBER b, PAY_KIND c WHERE a.ID = b.ID(+) AND c.SEQ(+) = a.PAY_CODE ");
		 
		
		
		if (hidden.equals("TODAY"))
		{
			queryBuffer.append("AND TO_DATE(a.TIME, 'YYYY-MM-DD') = TO_DATE(SYSDATE,'YYYY-MM-DD' ) ");
			countQuery.append("AND TO_DATE(a.TIME, 'YYYY-MM-DD') = TO_DATE(SYSDATE,'YYYY-MM-DD' ) ");
		}
		else if (hidden.equals("MONTH1"))
		{
			queryBuffer.append("AND TO_DATE( a.TIME, 'YYYY-MM-DD') BETWEEN  ADD_MONTHS(TO_DATE(SYSDATE,'YYYY-MM-DD' ), -1) AND  TO_DATE(SYSDATE, 'YYYY-MM-DD')  ");
			countQuery.append("AND TO_DATE( a.TIME, 'YYYY-MM-DD') BETWEEN  ADD_MONTHS(TO_DATE(SYSDATE,'YYYY-MM-DD' ), -1) AND  TO_DATE(SYSDATE, 'YYYY-MM-DD')  ");
		}
		else if (hidden.equals("MONTH2"))
		{
			queryBuffer.append("AND  TO_DATE( a.TIME, 'YYYY-MM-DD') BETWEEN  ADD_MONTHS(TO_DATE(SYSDATE,'YYYY-MM-DD' ), -2) AND  TO_DATE(SYSDATE, 'YYYY-MM-DD') ");
			countQuery.append("AND  TO_DATE( a.TIME, 'YYYY-MM-DD') BETWEEN  ADD_MONTHS(TO_DATE(SYSDATE,'YYYY-MM-DD' ), -2) AND  TO_DATE(SYSDATE, 'YYYY-MM-DD') ");
		}
		else if (hidden.equals("MONTH3"))
		{
			queryBuffer.append("AND TO_DATE( a.TIME, 'YYYY-MM-DD') BETWEEN  ADD_MONTHS(TO_DATE(SYSDATE,'YYYY-MM-DD' ), -3) AND  TO_DATE(SYSDATE, 'YYYY-MM-DD')  ");
			countQuery.append("AND TO_DATE( a.TIME, 'YYYY-MM-DD') BETWEEN  ADD_MONTHS(TO_DATE(SYSDATE,'YYYY-MM-DD' ), -3) AND  TO_DATE(SYSDATE, 'YYYY-MM-DD')  ");
		}
		else if (!date1.equals("") && !date2.equals(""))
		{
			queryBuffer.append("  AND  TO_CHAR(TIME, 'YYYY-MM-DD')>= '"+ date1+ "' AND TO_CHAR(TIME, 'YYYY-MM-DD')<='"+date2 +"'");
			countQuery.append("  AND  TO_CHAR(TIME, 'YYYY-MM-DD')>= '"+ date1+ "' AND TO_CHAR(TIME, 'YYYY-MM-DD')<='"+date2 +"'");
		}
		if (!id.equals(""))
		{
			queryBuffer.append(" AND a.ID = '"+ id +"'  ");
			countQuery.append(" AND a.ID = '"+ id +"'  ");
		}
		if (!option1.equals(""))
		{
			queryBuffer.append(" AND a.ORDER_STATE = '"+ option1 +"'  ");
			countQuery.append(" AND a.ORDER_STATE = '"+ option1 +"'  ");
		}
		if (!option2.equals(""))
		{
			queryBuffer.append("AND a.DELIVERY_STATE = '"+ option2 +"'  ");
			countQuery.append("AND a.DELIVERY_STATE = '"+ option2 +"'  ");
		}
		
		countQuery.append(" ORDER BY TIME DESC  ) a  GROUP BY a.DISP_NUM ");
		int inputPageNumber = Integer.parseInt(inputPage);



		DBExecute dbe = new DBExecute(countQuery.toString());
		outputList = dbe.getDataList();
		
		int count=0;
		if (outputList!=null && outputList.size()>0)
		{
			Hashtable countTable = (Hashtable)outputList.get(0);
		
			if ((String)countTable.get("COUNT") != null || ((String)countTable.get("COUNT")).length()>0)
			{
				
				count = Integer.parseInt((String)countTable.get("COUNT"));
			}
		}
		
		
		int end = count - (inputPageNumber *unit)+1;
		if (end<0)
		{
			end=unit;
		}
		else if (end < unit)
		{
			end=count;
		}
		int start = end-(unit-1);
		if(start<0)
		{
			start=1;
		}		
		queryBuffer.append(" ORDER BY TIME DESC ) a ");

		queryBuffer.append(" WHERE a.DISP_NUM BETWEEN '"+start+"' AND '"+end+ "' ");
		dbe = new DBExecute(queryBuffer.toString());
		outputList = dbe.getDataList();
		
	}
}